package ltu.m7011e.client.XMLParser;

import java.util.ArrayList;

import ltu.m7011e.shared.SharedConstants;
import ltu.m7011e.shared.model.Author;
import ltu.m7011e.shared.model.Link;

import com.google.gwt.core.client.GWT;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.NamedNodeMap;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;

public class Parser {

	/**
	 * An instance of the Shared Constants.
	 */
	private static final SharedConstants constants = GWT.create(SharedConstants.class);

	public static Document BuildLinkFromInfo(String linkUrl, 
			String linkCategory,
			String linkDescription,
			String linkAuthor){
		Document xmlLink = XMLParser.createDocument();
		Element quickShareCall = xmlLink.createElement(constants.cXmlQuickShareCall());
		Element linkElement = xmlLink.createElement(constants.cXmlLinkElementName());
		linkElement.setAttribute(constants.cXmlLinkAttUrl(), linkUrl);
		linkElement.setAttribute(constants.cXmlLinkAttCategory(), linkCategory);
		linkElement.setAttribute(constants.cXmlLinkAttDescription(), linkDescription);
		Element authorElement = xmlLink.createElement(constants.cXmlAuthorElementName());
		authorElement.setAttribute(constants.cXmlAuthorkAttName(), linkAuthor);
		xmlLink.appendChild(quickShareCall);
		quickShareCall.appendChild(linkElement);
		linkElement.appendChild(authorElement);

		return xmlLink;
	}

	public static String GetResponseAction(Document xmlToParse){
		String result = "";
		Document xmlDocument = xmlToParse;
		Node rootNode = xmlDocument.getFirstChild();
		Node secondNode = rootNode.getFirstChild();
		NamedNodeMap actionAttribute = secondNode.getAttributes();
		result = actionAttribute.getNamedItem(constants.cXmlActionElementNameAtt()).toString();
		GWT.log("[GetResponseAction] Result : "+result);
		return result;
	}

	public static ArrayList<Link> GetLinkArray(Document xmlToParse){
		ArrayList<Link> result = new ArrayList<Link>();

		Document xmlDocument = xmlToParse;
		Node rootNode = xmlDocument.getFirstChild();
		GWT.log("rootNode : "+rootNode.toString());
		Node secondNode = rootNode.getFirstChild();
		NodeList linksElement = secondNode.getChildNodes();
		for (int i=0; i< linksElement.getLength()  ;i++) {
			GWT.log("link:  : "+linksElement.item(i));
			Node link = linksElement.item(i);
			NamedNodeMap linkAttributes = link.getAttributes();
			String linkId = linkAttributes.getNamedItem(constants.cXmlLinkAttId()).toString();
			String linkUrl = linkAttributes.getNamedItem(constants.cXmlLinkAttUrl()).toString();
			String linkDateString = linkAttributes.getNamedItem(constants.cXmlLinkAttDate()).toString();
			String linkCategory = linkAttributes.getNamedItem(constants.cXmlLinkAttCategory()).toString();
			String linkLikes = linkAttributes.getNamedItem(constants.cXmlLinkAttLike()).toString();
			String linkDescription = linkAttributes.getNamedItem(constants.cXmlLinkAttDescription()).toString();
			Node author = link.getFirstChild();
			NamedNodeMap authorAttribute = author.getAttributes();
			String authorName = authorAttribute.getNamedItem(constants.cXmlAuthorkAttName()).toString();

			result.add(new Link(Integer.decode(linkId), linkUrl, new Author(authorName), linkCategory, linkDescription, Integer.decode(linkLikes), linkDateString ));

		}

		return result;
	}


	public static Author GetUserInfo(Document xmlToParse){
		Author loginInfo;
		Document xmlDocument = xmlToParse;
		Node rootNode = xmlDocument.getFirstChild();
		GWT.log("rootNode : "+rootNode.toString());
		Node secondNode = rootNode.getFirstChild();
		Node userElement = secondNode.getFirstChild();

		NamedNodeMap linkAttributes = userElement.getAttributes();
		String nickname = linkAttributes.getNamedItem(constants.cXmlUserInfoAttNickname()).toString();
		Boolean loggedIn = Boolean.valueOf(linkAttributes.getNamedItem(constants.cXmlUserInfoAttLoggedIn()).toString());
		String loginUrl = linkAttributes.getNamedItem(constants.cXmlUserInfoAttLoginUrl()).toString();
		String logoutUrl = linkAttributes.getNamedItem(constants.cXmlUserInfoAttLogoutUrl()).toString();
		String emailAddress = linkAttributes.getNamedItem(constants.cXmlUserInfoAttEmailAddress()).toString();

		loginInfo = new Author(nickname, loggedIn, loginUrl, logoutUrl, emailAddress);		

		return loginInfo;
	}

}
